﻿using FR.CodeVisualizer.Controls.TreeList;
using FR.CodeVisualizer.ObjectSource;
using FR.CodeVisualizer.Visualizers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FR.CodeVisualizer.Views
{
    public partial class FormCollectionVisualizer : Form
    {
        public FormCollectionVisualizer()
        {
            InitializeComponent();
        }

        public string FilterName
        {
            get { return txtFilterName.Text; }
            set { txtFilterName.Text = value; }
        }

        public string FilterValue
        {
            get { return txtFilterValue.Text; }
            set { txtFilterValue.Text = value; }
        }

        public string FilterType
        {
            get { return txtFilterType.Text; }
            set { txtFilterType.Text = value; }
        }

        public int Depth
        {
            get { return (int)numericDepth.Value; }
            set
            {
                if (RefectionOptionsContainer.Instance.Options.Depth != value)
                {
                    RefectionOptionsContainer.Instance.Options.Depth = value;
                    RefectionOptionsContainer.Instance.SaveOptions();
                }

                numericDepth.Value = value;
            }
        }

        public void Init(List<Entity> entities)
        {
            collectionViewTree.InitCollection(entities);
            UpdateEntitiesCount();
            Depth = RefectionOptionsContainer.Instance.Options.Depth;
        }

        private void txtFilterName_TextChanged(object sender, EventArgs e)
        {
            collectionViewTree.FilterName(this.FilterName, CollectionViewTree.ColumnType.Name);
            UpdateEntitiesCount();
        }

        private void txtFilterType_TextChanged(object sender, EventArgs e)
        {
            collectionViewTree.FilterName(this.FilterType, CollectionViewTree.ColumnType.Type);
            UpdateEntitiesCount();
        }

        private void txtFilterValue_TextChanged(object sender, EventArgs e)
        {
            collectionViewTree.FilterName(this.FilterValue, CollectionViewTree.ColumnType.Value);
            UpdateEntitiesCount();
        }

        private void UpdateEntitiesCount()
        {
            this.toolStripEntitiesCount.Text = string.Format("{0} Nodes", collectionViewTree.EntitiesCount);
        }


        #region Menu events
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void clearFiltersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(FilterName))
                FilterName = string.Empty;

            if (!string.IsNullOrEmpty(FilterValue))
                FilterValue = string.Empty;

            if (!string.IsNullOrEmpty(FilterType))
                FilterType = string.Empty;
        }

        private void collapseAllNodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.collectionViewTree.BeginUpdate();
            foreach (Node node in this.collectionViewTree.Nodes)
            {
                node.Collapse();
            }
            this.collectionViewTree.EndUpdate();
        }

        private void expandAllNodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.collectionViewTree.BeginUpdate();
            foreach (Node node in this.collectionViewTree.Nodes)
            {
                node.Expand();
            }
            this.collectionViewTree.EndUpdate();
        }

        private void exportExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.collectionViewTree.ExportToExcel();
        }

        private void exportXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.collectionViewTree.ExportToXml();
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AboutCollectionsVisualizer about = new AboutCollectionsVisualizer();
            about.ShowDialog();
        } 
        #endregion

        private void numericDepth_ValueChanged(object sender, EventArgs e)
        {
            Depth = (int)numericDepth.Value;
        }
    }
}
